package kylin.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.apache.log4j.Logger;

public class ImportExcel {

	Logger log = Logger.getLogger(ImportExcel.class);
	
	
	/**
	 * 读取Excel数据
	 * @param fileUrl
	 * @return
	 */
	public List<List<Object>> importExcel(String fileUrl){
		File file = new File(fileUrl);

		List<List<Object>> list = new ArrayList<List<Object>>();
		
		try {
			Workbook book = Workbook.getWorkbook(file);
			
			Sheet sheet = book.getSheet(0);//获取第一张工作表
			
			for (int i = 2; i < sheet.getRows(); i++) {//从第二行开始读取数据
				List<Object> rowList = new ArrayList<Object>();
				for (int j = 0; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(j, i);
					rowList.add(cell.getContents());
					//System.out.print(cell.getContents() + "\t");
				}
				list.add(rowList);
				//System.out.println();
			}
			book.close();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
		log.info("读取数量："+list.size());
		
		return list;
		
	}

	
	public static void main(String[] args) {
		List<List<Object>> list = new ArrayList<List<Object>>();
        try {
            Workbook book = Workbook.getWorkbook(new File("c:\\test.xls"));
            // 获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            // 得到单元格
            
            for (int i = 2; i < sheet.getRows(); i++) {//从第二行开始读取数据
				List<Object> rowList = new ArrayList<Object>();
				for (int j = 0; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(j, i);
					rowList.add(cell.getContents());
					//System.out.print(cell.getContents() + "\t");
				}
				list.add(rowList);
				//System.out.println();
			}
            
            book.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
	
}
